package com.edu.cq02.controller;


import com.edu.cq02.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
@SessionAttributes(value = "userinfo1")
public class loginController {
    @RequestMapping("tologin")
    public String tologin() {
        return "login";
    }

    @RequestMapping("login")
    public String login(HttpSession httpSession) {
        User user = new User();
        user.setUsername("张三");
        user.setPassword("1111");
        httpSession.setAttribute("filename", user);

        httpSession.setAttribute("userinfo", user);
        return "home";
    }

    @RequestMapping("login1")
    public String login1(ModelMap modelMap, HttpSession httpSession) {
        User user = new User();
        user.setUsername("张三");
        user.setPassword("1111");
        httpSession.setAttribute("userinfo", user);
        //必须和@SessionAttributes(value = "userinfo1") key对应上
        modelMap.addAttribute("userinfo1", user);
        return "home";
    }

    @RequestMapping("login2")
    public String login2(ModelMap modelMap) {
        User user = new User();
        user.setUsername("张三");
        user.setPassword("1111");
        //必须和@SessionAttributes(value = "userinfo1") key对应上
        modelMap.addAttribute("userinfo1", user);
        modelMap.addAttribute("userinfo2", user);

        return "home";
    }

    @RequestMapping("login3")
    public String login3() {
        ModelMap modelMap = new ModelMap();
        User user = new User();
        user.setUsername("张三");
        user.setPassword("1111");
        //必须和@SessionAttributes(value = "userinfo1") key对应上
        modelMap.addAttribute("userinfo1", user);
        modelMap.addAttribute("userinfo2", user);

        return "home";
    }

    @RequestMapping("login4")
    public ModelAndView login4() {
        ModelAndView modelAndView = new ModelAndView();
        User user = new User();
        user.setUsername("张三");
        user.setPassword("1111");
        modelAndView.addObject("userinfo1", user);
        modelAndView.setViewName("home");
        return modelAndView;
    }

    @RequestMapping("remove")
    public String remove(SessionStatus sessionStatus, HttpSession httpSession) {
        httpSession.removeAttribute("userinfo");
        sessionStatus.setComplete();
        return "home";
    }

}

